﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BrandUnion.CustomData;

public partial class ManageBookings : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsSlots.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsSlots_Select);
        grid.DetailTableDataBind += new GridDetailTableDataBindEventHandler(grid_DetailTableDataBind);
        grid.UpdateCommand += new GridCommandEventHandler(grid_UpdateCommand);
        grid.DeleteCommand += new GridCommandEventHandler(grid_DeleteCommand);
        grid.ItemDataBound += new GridItemEventHandler(grid_ItemDataBound);
    }

    void grid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.IsDataBound && e.Item.OwnerTableView.ParentItem != null && e.Item is GridDataItem)
        {
            Booking booking = e.Item.DataItem as Booking;
            if (booking != null && booking.UserId != null)
            {
                GridEditableItem tmp = e.Item as GridEditableItem;
                var disable = e.Item.OwnerTableView.Columns.OfType<Acms.WebControls.RadGridEditColumn>().ToList();
                foreach (var item in disable)
            	{
                    tmp[item].Enabled = false;
                    tmp[item].Controls.Clear();
            	}
            }
        }
    }

    void grid_DeleteCommand(object source, GridCommandEventArgs e)
    {
        var id = (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
        BrandUnion.CustomData.BookingFactory factory = new BrandUnion.CustomData.BookingFactory();
        var booking = factory.GetById(id);
        if (booking != null)
        {
            factory.Delete(booking);
            grid.Rebind();
            grid.Items[e.Item.OwnerTableView.ParentItem.ItemIndex].Expanded = true;
        }
        
    }

    void grid_UpdateCommand(object source, GridCommandEventArgs e)
    {
        var id = (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"];
        BrandUnion.CustomData.BookingFactory factory = new BrandUnion.CustomData.BookingFactory();
        GridEditableItem editItem = e.Item as GridEditableItem;
        var booking = factory.GetById(id);
        if (booking != null)
        {
            Hashtable newValues = new Hashtable();
            editItem.ExtractValues(newValues);
            booking.Address1 = (string)newValues["Address1"];
            booking.Address2 = (string)newValues["Address2"];
            booking.County = (string)newValues["County"];
            booking.EmailAddress = (string)newValues["EmailAddress"];
            booking.FirstName = (string)newValues["FirstName"];
            booking.LastName = (string)newValues["LastName"];
            booking.PostCode = (string)newValues["PostCode"];
            booking.Tel = (string)newValues["Tel"];
            booking.Town = (string)newValues["Town"];
            try
            {
                booking.Age = int.Parse((string)newValues["Age"]);
            }
            catch (FormatException) { }
            try
            {
                booking.MoreInfo = (bool)newValues["MoreInfo"];
            }
            catch (FormatException) { }

            factory.Save(booking);
            e.Item.Edit = false;
        }
        else
        {
            e.Canceled = true;
        }
    }


    void grid_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
    {
        var index = e.DetailTableView.ParentItem.ItemIndex;
        int id = (int)grid.MasterTableView.DataKeyValues[index]["Id"];
        BrandUnion.CustomData.BookingFactory factory = new BrandUnion.CustomData.BookingFactory();
        var data = factory.GetAllByBookingSlotId(id);
        e.DetailTableView.DataSource = data;
    }

    void dsSlots_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        BrandUnion.CustomData.BookingSlotFactory factory = new BrandUnion.CustomData.BookingSlotFactory();
        e.SetData(factory.GetAll());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var changeSlotCol = grid.MasterTableView.DetailTables[0].Columns.FindByUniqueName("ChangeSlot") as GridHyperLinkColumn;
            if (changeSlotCol != null)
            {
                changeSlotCol.DataNavigateUrlFormatString = base.ResolveCustomControlPath("~/BookingDetail.ascx?Id={0}");
            }
        }
    }

}
